'use strict'
const whiteList = require('../lib/whiteList')
const categoryServices = require('../services').category
const category = {}

// 获取所有分类
category.getAllCategory = async (ctx, next) => {
    const result = await categoryServices.getAllCategory()
    ctx.result = result
    return next()
}

// 创建分类
category.createCategory = async (ctx, next) => {
    whiteList.needAdmin(ctx, next)
    const categoryList = ctx.request.body
    const result = await categoryServices.createCategory(categoryList)
    console.log(result);
    ctx.result = result
    return next()
}

// 删除分类
category.deleteCategory = async (ctx, next) => {
    whiteList.needAdmin(ctx, next)
    const categoryList = ctx.request.body
    const result = await categoryServices.deleteCategory(categoryList)
    ctx.result = result
    console.log(result)
    return next()
}

// 编辑一个分类
category.updateOneCategory = async (ctx, next) => {
    whiteList.needAdmin(ctx, next)
    const category = ctx.request.body
    const result = await categoryServices.updateOneCategory(category._id, category)
    ctx.result = result
    console.log(result)
    return next()
}

module.exports = category
